﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpGL.SceneGraph.Assets;
using SharpGL;
using System.IO;

namespace bitzhuwei.FeedingFrenzy.WorldModel
{
    public class GameBoard : GameObject
    {
        public override string ToString()
        {
            return string.Format("{0}", GameBoard.texture != null ? GameBoard.texture.Name : "null");
            //return base.ToString();
        }
        public static void LoadTextures(string filename, OpenGL gl)
        {
            try
            {
                if (GameBoard.texture != null)
                {
                    GameBoard.texture.Destroy(gl);
                }
                Texture t = new Texture();
                t.Create(gl, filename);
                t.Name = filename;
                GameBoard.texture = t;
            }
            catch (Exception)
            {
            }
        }
        public static void Dispose(OpenGL gl)
        {
            if (gl != null && GameBoard.texture != null)
            {
                GameBoard.texture.Destroy(gl);
            }
        }

        public static GameBoard GetInstance()
        {
            GameBoard result = new GameBoard();
            return result;
        }

        public override void Draw(SharpGL.OpenGL gl)
        {
            //  Load the identity matrix.
            var y = -1;
            for (int i = World.MinScope.X; i < World.MaxScope.X; i += 1)
            {
                for (int j = World.MinScope.Z; j < World.MaxScope.Z; j += 1)
                {
                    gl.LoadIdentity();
                    gl.Translate(i, y, j);
                    //gl.BindTexture(OpenGL.GL_TEXTURE_2D, GameBoard.texture.TextureName);
                    GameBoard.texture.Bind(gl);
                    gl.Color(1f, 1, 1, 1);
                    gl.Begin(OpenGL.GL_TRIANGLE_STRIP);				// Build Quad From A Triangle Strip
                    gl.TexCoord(1, 1); gl.Vertex(i + 1f, y, j + 1f); // Top Right
                    gl.TexCoord(0, 1); gl.Vertex(i - 1f, y, j + 1f); // Top Left
                    gl.TexCoord(1, 0); gl.Vertex(i + 1f, y, j - 1f); // Bottom Right
                    gl.TexCoord(0, 0); gl.Vertex(i - 1f, y, j - 1f); // Bottom Left
                    gl.End();						// Done Building Triangle Strip
                }
            }
            gl.Flush();
            //base.Draw(gl);
        }

        protected GameBoard()
        {
        }

        static Random rand = new Random();
        private static Texture texture;

        public override void Update()
        {
            //base.Update();
        }

    }
}
